const Service = require("egg").Service;

const columns = [
  "id",
  "name",
  "cateid",
  "catename",
  "author",
  "on_shelf",
  "publishdate",
  "createtime",
  "updatetime",
  "press",
  "info"
]

class BookService extends Service {
  // 图书入库
  async storage(params) {
    const { ctx, app } = this;
    // 查分类
    const res = await app.mysql.select("book_cate", {
      where: {
        book_cate_id: params.cateid,
      },
    });
    // 分类信息
    const cateinfo = res[0];
    console.log(cateinfo);
    const insertRes = await app.mysql.insert("books_list", {
      id: ctx.uuid(),
      name: params.name,
      author: params.author,
      cateid: cateinfo.book_cate_id,
      catename: cateinfo.book_cate_name,
      catevalue: cateinfo.book_cate_value,
      press: params.press,
      info: params.info,
      publishdate: new Date(params.publishdate),
      on_shelf: 2, // 1 已上架  2 已下架
      is_delete: 1, // 0 已删除   1 未删除
      createtime: ctx.cTime(),
      updatetime: ctx.cTime(),
    });
    return insertRes;
  }
  // 重新入库
  async reStorage(params) {
    const { app } = this;
    return await app.mysql.update(
      "books_list",
      {
        is_delete: 1,
        on_shelf: 2,
        createtime: this.ctx.cTime(),
      },
      {
        where: {
          id: params.id,
        },
      }
    );
  }
  // 图书列表
  async list(params) {
    const { ctx, app } = this;
    // 查询数据总数
    const total = await app.mysql.select("books_list", {
      where: {
        is_delete: 1
      }
    });
    
    let queryStr;

    const condition = ctx.condition(params)
    console.log(condition);

    if(condition.has){
      queryStr = `select ${columns.join(',')} from books_list where ${condition.str} and is_delete=1 order by createtime desc limit ${
        params.pagesize
      } offset ${params.pagesize * params.pagenum - params.pagesize}`
    }else {
      queryStr = `select ${columns.join(',')} from books_list where is_delete=1 order by createtime desc limit ${
        params.pagesize
      } offset ${params.pagesize * params.pagenum - params.pagesize}`
    }

    const data = await app.mysql.query(queryStr);
    return { data, total: total.length };
  }
  // 图书已删除列表
  async listDel(params) {
    const { ctx, app } = this;
    // 查询数据总数
    const total = await app.mysql.select("books_list", {
      where: {
        is_delete: 0
      }
    });

    let queryStr;

    const condition = ctx.condition(params)
    console.log(condition);

    if(condition.has){
      queryStr = `select ${columns.join(',')} from books_list where ${condition.str} and is_delete=0 order by createtime desc limit ${
        params.pagesize
      } offset ${params.pagesize * params.pagenum - params.pagesize}`
    }else {
      queryStr = `select ${columns.join(',')} from books_list where is_delete=0 order by createtime desc limit ${
        params.pagesize
      } offset ${params.pagesize * params.pagenum - params.pagesize}`
    }

    const data = await app.mysql.query(queryStr)
    return { data, total: total.length };
  }
  // 图书删除
  async delete(params) {
    const { app } = this;
    return await app.mysql.update(
      "books_list",
      {
        is_delete: 0,
        updatetime: this.ctx.cTime(),
      },
      {
        where: {
          id: params.id,
        },
      }
    );
  }
  // 图书彻底删除
  async deleteRel(params) {
    const { app } = this;
    return await app.mysql.delete("books_list", {
      where: {
        id: params.id,
      },
    });
  }
  // 图书更新
  async update(params) {
    const { ctx, app } = this;
    // 查分类
    const res = await app.mysql.select("book_cate", {
      where: {
        book_cate_id: params.cateid,
      },
    });
    // 分类信息
    const cateinfo = res[0];
    return await app.mysql.update(
      "books_list",
      {
        name: params.name,
        author: params.author,
        cateid: params.cateid,
        catename: cateinfo.book_cate_name,
        catevalue: cateinfo.book_cate_value,
        press: params.press,
        info: params.info,
        publishdate: new Date(params.publishdate),
        updatetime: ctx.cTime(),
      },
      {
        where: {
          id: params.id,
        },
      }
    );
  }
  // 图书上架
  async shlef(params) {
    const { ctx, app } = this;
    return await app.mysql.update(
      "books_list",
      {
        on_shelf: params.on_shelf,
        updatetime: ctx.cTime(),
      },
      {
        where: {
          id: params.id,
        },
      }
    );
  }
}

module.exports = BookService;
